import tornado
import requests
import tornado.httpclient
import pandas as pd
import json
import time
from HttpService import IsTest
# 训练数据规则
ruleNameTraindata = 'AI_BUV1_training_set'
# 逆文档频率衰减规则
ruleNameTF_IDF = 'AI_BUV1_Label_Gmodel_Num'
# 墙面数据规则
ruleNameWallTrainData = 'TD_BUV1_QiangmianNormal'
# 背景墙训数据
ruleNameBackgroundWallTrainData = 'TR_custom_layout'

host = "http://192.168.1.114:8281/DSORM/openService.do"
formalHost = "http://dsorm.youjiagou.com/openService.do"
if IsTest:
	pass
else:
	host = formalHost
serviceName = "{\"action\":\"getAction\",\"method\":\"get\",\"param\":{\"param\":{\"limit_start\":\"%s\",\"limit_end\":\"%s\",\"random\":\"%s\"},\"format\":\"json\",\"serviceName\":\"%s\",\"needAll\":\"1\"}}"

# 根据分页获取请求数据
def gettraindata (s, e, servername):
	param = serviceName % (s, e, int(time.time()), servername)
	url = "%s?%s" % (host, param)
	response = requests.get(url)
	try:
		if response.ok:
			return response.text
		else:
			return None
	except:
		return None

# 解析请求数据
def parsetraindata (s, e, servername):
	data = gettraindata(s,e,servername)
	if data is None:
		return None
	else:
		obj = None
		try:
			obj = json.loads(data)
		except Exception as ex:
			print("解析出错%s"%(ex,))
		if obj is None or "rows" not in obj:
			return None
		else:
			rows = obj.get("rows");
			return rows

# 获取所有数据
def alldata(servername,callBack):
	rets=[]
	s,e=0,1000
	l=e
	IsOnce = False

	while True:
		_rets = parsetraindata(s, e, servername)
		if _rets is None or len(_rets) == 0:
			break
		else:
			rets += _rets
			if len(_rets) < l:
				break
		if callBack:
			callBack(s, e)
		s = s + l
		if IsOnce:
			break

	return rets

#训练数据转换成excel格式
def convertdatatoexcel ():
	return convertfurnituredatatoexcel()

def convertfurnituredatatoexcel():
	return _convertdatatoexcel(ruleNameTraindata)

def convertwalldatatoexcel ():
	return _convertdatatoexcel(ruleNameWallTrainData)

def convertBackgroundWallDataToExcel ():
	return _convertdatatoexcel(ruleNameBackgroundWallTrainData)

# 训练数据转换成excel
def _convertdatatoexcel(serviceName):
	rets = alldata(serviceName, lambda x, y: print(x, y))
	contents = []
	titles = []
	for item in rets:
		temp = ()
		for k, v in item.items():
			if k not in titles:
				titles.append(k)
			temp += (v,)
		contents += [temp]
	return pd.DataFrame(contents, columns=titles)

#获取所有逆文档衰减率
def allTF_IDF():
	array = alldata(ruleNameTF_IDF, lambda x, y: print(x,y,"idocument"))
	result,_allCount,finalResult = {},{},{}
	if len(array) != 0:
		for item in array:
			typeName = item.get ('type')

			labelName = item.get ('label_name')
			num = item.get ('gmodel_num')
			try:
				num = int(num)
			except:
				continue
			if num <= 0:
				num = 1
			if not typeName or not labelName:
				continue
			if typeName not in result:
				result[typeName] = {}
			_dict = result[typeName]
			if labelName in _dict:
				_dict[labelName] = _dict[labelName] + num
			else:
				_dict[labelName] = num

			if typeName not in _allCount:
				_allCount [typeName] = num
			else:
				_allCount [typeName] = _allCount[typeName] + num

		for typeName, cnt in _allCount.items():
			if typeName not in result:
				continue
			resultVal = result.get(typeName)

			for rkey in resultVal.keys():
				finalResult[(typeName, rkey)] = round(cnt / (1 + resultVal[rkey]), 2)

	return finalResult

def solraction():
	global host
	actionstring = "{\"action\":\"getAction\",\"method\":\"get\",\"param\":{\"format\":\"json\",\"serviceName\":\"BUV1_solr_structuring_info\",\"needAll\":\"1\"}}"
	URL = "%s?%s"%(host, actionstring)
	res = requests.get(URL)
	if res.ok:
		result = json.loads(res.text)
		if 'code' in result and result['code'] == "0" and 'rows' in result:
			rows = result['rows']
			if type(rows) == type([]) and len(rows) != 0 and 'solr_structuring_info' in rows[0]:
				return rows[0].get('solr_structuring_info')
			else:
				return ""
		else:
			return ""
	else:
		return ""

def TrainDataColumns ():
	global host
	action = {
		"action": "getAction",
		"method": "get",
		"param": {
			"format": "json",
			"serviceName": "ai_training_column",
			"needAll": "1"
		}
	}
	URL = "%s?%s" % (host, json.dumps(action))
	res = requests.get(URL)
	result = []
	if res.ok:
		jData = json.loads(res.text)
		if 'code' in jData and jData['code'] == "0" and 'rows' in jData:
			rows = jData['rows']
			if type(rows) == type([]) and len(rows) != 0 and "training_column" in rows[0]:
				try:
					val = rows[0].get("training_column")
					result = json.loads(val)
				except:
					pass
	return result

def SklearnTypeList():
	global host
	action = {
			  "action": "getAction",
			  "method": "get",
			  "param": {
				"format": "json",
				"serviceName": "BUV1_ai_bayes_type",
				"needAll": "1"
			  }
			}
	URL = "%s?%s" % (host, json.dumps(action))
	res = requests.get(URL)
	result = []
	if res.ok:
		jData = json.loads(res.text)
		if 'code' in jData and jData['code'] == "0" and 'rows' in jData:
			rows = jData['rows']
			if type(rows) == type([]) and len(rows) != 0:
				_key = "type_value"
				try:
					for row in rows:
						if _key not in row:
							continue
						_val = row.get(_key)
						result.append(int(_val))
				except:
					pass
	return result

def solrActionVersion1 ():
	global host
	actionstring = "{\"action\":\"getAction\",\"method\":\"get\",\"param\":{\"format\":\"json\",\"serviceName\":\"BUV1_solr_structuring_info_original\",\"needAll\":\"1\"}}"
	URL = "%s?%s" % (host, actionstring)
	res = requests.get(URL)
	if res.ok:
		result = json.loads(res.text)
		if 'code' in result and result['code'] == "0" and 'rows' in result:
			rows = result['rows']
			if type(rows) == type([]) and len(rows) != 0 and 'solr_structuring_info' in rows[0]:
				return rows[0].get('solr_structuring_info')
			else:
				return ""
		else:
			return ""
	else:
		return ""